"""此文件用于渲染生物群系特有色彩"""

import pygame
import config
import random as r
from typing import Union

square : tuple = (1650, 920)
__is_inited: bool = False
biome_render : pygame.surface.Surface = pygame.Surface(square)

def cint(d) -> int:
    return d if type(d) is int else int(d)

def prob(p : int) -> bool:
    if r.randint(0, 100) < p:
        return True
    return False

class Ocean(object):
    def __init__(self, bg: pygame.surface.Surface) -> None:
        self.__biome_render : pygame.surface.Surface = pygame.Surface(square)
        self.__biome_render.fill((12, 16, 24))
        self.new_bg : pygame.surface.Surface = bg.copy()
        self.new_bg.blit(self.__biome_render, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
    
    def render_biome(self) -> None:
        config.screen.blit(self.__biome_render, (0, 0), special_flags=pygame.BLEND_RGB_ADD)

class Deep_Ocean(object):
    def __init__(self, bg: pygame.surface.Surface) -> None:
        self.__biome_render : pygame.surface.Surface = pygame.Surface(square)
        self.__biome_render.fill((0, 0, 4))
        self.__biome_render_2 : pygame.surface.Surface = pygame.Surface(square)
        self.__biome_render_2.fill((8, 4, 0))
        self.new_bg : pygame.surface.Surface = bg.copy()
        self.new_bg.blit(self.__biome_render, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
        self.new_bg.blit(self.__biome_render_2, (0, 0), special_flags=pygame.BLEND_RGB_SUB)
    
    def render_biome(self) -> None:
        config.screen.blit(self.__biome_render, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
        config.screen.blit(self.__biome_render_2, (0, 0), special_flags=pygame.BLEND_RGB_SUB)

class Warm_Ocean(object):
    def __init__(self, bg : pygame.surface.Surface, gradient_method: str = 'smooth') -> None:
        self.gradient_method: str
        if gradient_method in ('smooth', 'random'):
            self.gradient_method = gradient_method
        else:
            self.gradient_method = 'random' if config.biomefilter['Warm_Ocean']['smooth'] == False else 'smooth'
        self.__biome_render: pygame.surface.Surface = pygame.Surface(square)
        if self.gradient_method == 'smooth':
            biome_render_smooth :pygame.surface.Surface = pygame.surface.Surface((2, 2))
            biome_render_smooth.set_at((0, 0), (255, 128, 100))
            biome_render_smooth.set_at((1, 0), (128, 255, 100))
            pygame.draw.line(biome_render_smooth, (211, 211, 180), (0, 1), (1, 0))
            self.__biome_render = pygame.transform.smoothscale(biome_render_smooth, square)
        else:
            for x in range(square[0]):
                for y in range(square[1]):
                    red, green = r.randint(0, cint(min(max((255 - x) * (920 - y) / 920, 1), 255))), r.randint(0, cint(min(max((x * (920 - y) / 920), 1), 255)))
                    self.__biome_render.set_at((x, y), (red, green, 0))
        self.__biome_render.set_alpha(config.biomefilter['Warm_Ocean']['α'])
        
        self.new_bg : pygame.surface.Surface = bg.copy()
        self.new_bg.blit(self.__biome_render, (0, 0))
    
    def render_biome(self) -> None:
        config.screen.blit(self.__biome_render, (0, 0))

class Cold_Ocean(object):
    def __init__(self, bg: pygame.surface.Surface) -> None:
        self.__biome_render : pygame.surface.Surface = pygame.Surface(square)
        self.__biome_render.fill((8, 12, 32))
        self.new_bg : pygame.surface.Surface = bg.copy()
        self.new_bg.blit(self.__biome_render, (0, 0), special_flags=pygame.BLEND_RGB_ADD)
    
    def render_biome(self) -> None:
        config.screen.blit(self.__biome_render, (0, 0), special_flags=pygame.BLEND_RGB_ADD)

def set_biome(biome: str, bg: pygame.surface.Surface) -> Union[None, Ocean, Deep_Ocean, Warm_Ocean, Cold_Ocean]:
    global __is_inited
    __is_inited = True
    if biome == 'ocean':
        return Ocean(bg)
    elif biome == 'deep_ocean':
        return Deep_Ocean(bg)
    elif biome == 'warm_ocean':
        return Warm_Ocean(bg)
    else:
        __is_inited = False
        return None

def uninstall() -> None:
    global __is_inited
    __is_inited = False

class Dusk(object):
    """绘制黄昏光晕"""
    def __init__(self, biome:Union[str, None] = None) -> None:
        self.biome = biome
        self.duskpic : list = []
        for i in range(1, 101):
            # white->yellow
            p = pygame.Surface((1650, 2))
            p.fill((128,128,160))
            pygame.draw.line(p, (255, 255, 0), (0, 0), (1650, 0))
            p = pygame.transform.smoothscale(p, (1650, 920))
            p.set_alpha(cint(i*1.5))
            self.duskpic.append(p)
        for i in range(1, 101):
            p = pygame.Surface((1650, 2))
            p.fill((128, 128, 160))
            pygame.draw.line(p, (255, cint(255 - i*2.4), 0), (0,0), (1650, 0))
            p = pygame.transform.smoothscale(p, (1650, 920))
            p.set_alpha(150)
            self.duskpic.append(p)
        for i in range(1, 101):
            p = pygame.Surface((1650, 2))
            p.fill((128-i, 128-i, 160-i))
            pygame.draw.line(p, (255-i, 10, 0), (0,0), (1650, 0))
            p = pygame.transform.smoothscale(p, (1650, 920))
            p.set_alpha(max(cint(156 - i * 1.5), 64))
            self.duskpic.append(p)
    def draw(self, sunrise_time):
        tt = cint((pygame.time.get_ticks() - sunrise_time - 546_000) / 200)
        if tt < 0:
            tt = 0
        elif tt >= 300:
            tt = 299
        config.screen.blit(self.duskpic[tt], (0,0))